﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NFL.Loader
{
    public class DataLoad
    {
        public bool LoadWeeklyGames(int weekID, string[] fileData)
        {
            try
            {
                DataAccess.DataAccess da = new DataAccess.DataAccess();

                List<DataAccess.Game> games = new List<DataAccess.Game>();

                // Get the games from file load into list
                foreach (var row in fileData)
                {
                    if (!string.IsNullOrEmpty(row.Trim()) && row.Contains('@'))
                    {
                        DataAccess.Game g = new DataAccess.Game();
                        string awayTeam = row.Split('@')[0].Trim();
                        string homeTeam = row.Split('@')[1].Trim();

                        // Fix names
                        if (awayTeam.ToLower() == "indy")
                        {
                            awayTeam = "IND";
                        }
                        if (homeTeam.ToLower() == "indy")
                        {
                            homeTeam = "IND";
                        }

                        g.AwayTeam = da.GetTeamID(awayTeam);
                        g.HomeTeam = da.GetTeamID(homeTeam);
                        games.Add(g);
                    }
                }

                // Loop through list and persist games
                int sortOrder = 0;
                foreach (var game in games)
                {
                    sortOrder++;
                    game.SortOrder = sortOrder;
                    game.WeekID = weekID;
                    da.PersistGame(game);
                }

                return true;
            }
            catch 
            {
                return false;
            }
        }



        public bool LoadWeeklyPicks(int weekID, string[] fileData)
        {
            try
            {
                DataAccess.DataAccess da = new DataAccess.DataAccess();

                List<DataAccess.Pick> picks = new List<DataAccess.Pick>();

                foreach (var row in fileData)
                {
                    string[] columnData = row.Split(',');

                    if (!string.IsNullOrEmpty(columnData[0]))
                    {

                        // Get user ID
                        int UserID = da.GetUserID(columnData[0].ToString());

                        for (int i = 2; i < columnData.Length; i++)
                        {
                            // Picks are all alpha
                            if (!string.IsNullOrEmpty(columnData[i].ToString().Trim()) && columnData[i].ToString().All(char.IsLetter))
                            {
                                DataAccess.Pick p = new DataAccess.Pick();
                                p.UserID = UserID;
                                
                                // Get game ID 
                                p.GameID = da.GetGameID(weekID, i - 1);

                                p.TeamID = da.GetTeamID(columnData[i].ToString());

                                
                                // Check one ahead if its numeric, thats monday points and exit   brea;
                                int mondayPoints = 0;
                                if (int.TryParse(columnData[i + 1], out mondayPoints))
                                {
                                    p.TotalPoints = mondayPoints;
                                    picks.Add(p);
                                    break;
                                }

                                picks.Add(p);
                            }
                        }



                    }
                }

                // Persist picks to DB
                foreach (var pick in picks)
                {
                    da.PersistPick(pick);
                }


                return true;
            }
            catch 
            {
                return false;
            }
        }



    }
}
